<?php
if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Resep extends CI_Controller {

	function __construct() {
		parent::__construct();
		$this->load->library('breadcrumb');
		$this->load->model('m_resep');
	}

	function index() {
		show_404();
	}

	public function resep_add($KdKunjungan){
		if($KdKunjungan != ""){
			if(isset($_POST['btn_simpan'])){
				$this->m_resep->store_resep($KdKunjungan, $_POST);
				redirect($_SERVER['HTTP_REFERER'],'refresh');
			}else{
				$this->load->helper('cek_id');
				if(!cek_id($KdKunjungan, 'KdKunjungan', 'kunjungan')){
					show_404();
				}

				$this->layout->set_include('js', 'themes/apotek/js/jquery-ui.js', 'header');
				$this->layout->set_include('js', 'themes/apotek/js/datepicker-id.js', 'header');
				$this->layout->set_include('css', 'themes/apotek/css/olide-datepicker.css');
				$this->layout->set_include('css', 'themes/apotek/css/olide-custom.css');

				$data['kunjungan'] = $this->m_resep->get_kunjungan_byid($KdKunjungan, $_POST);
				$data['KdKunjungan'] = $KdKunjungan;
				$this->layout->theme('apotek','resep/index', $data);
			}
		}else{
			show_404();
		}
	}

	public function store_resep($KdKunjungan){
		if(isset($_POST)){
			echo "string";
		}
	}

	public function destroy_obat_resep($id){
		$this->load->helper('cek_id');
		if(!cek_id($id, 'KdObatResep', 'obat_resep')){
			set_flash('danger','Ups, data resep obat tidak ditemukan');
			redirect($_SERVER['HTTP_REFERER'],'refresh');
		}
		$this->m_resep->destroy_obat_resep($id);
		redirect($_SERVER['HTTP_REFERER'],'refresh');
	}

	public function update_obat_resep(){
		$KdObatResep  = $this->input->post('pk');
		$dataUpdate = array(
			'KdObat' => $this->input->post('value'), 
			);

		$this->m_resep->update_obat_resep($KdObatResep, $dataUpdate);
	}

	public function resep_edit($id){
		$judul = "Edit Resep";
		$data['judul'] = $judul;
		$this->layout->set_title($judul);
		$this->layout->set_desc("lorem ipsum");

		$this->breadcrumb->clear();
		$this->breadcrumb->add_crumb('Dasbor', site_url('dasbor'));
		$this->breadcrumb->add_crumb('Resep', site_url('#'));
		$this->breadcrumb->add_crumb($judul);

		$data['resep'] = $this->m_resep->get_resep_byid($id);

		$this->layout->theme('apotek','resep/edit', $data);
	}

	public function resep_update($id){
		$this->load->helper('cek_id');
		if(!cek_id($id, 'KdResep', 'resep')){
			set_flash('danger','Ups, data resep tidak ditemukan');
			redirect($_SERVER['HTTP_REFERER'],'refresh');
		}

		$this->m_resep->update_resep($_POST, $id);
		redirect($_SERVER['HTTP_REFERER'],'refresh');
	}
}
